
/**
 *        节点 特效 
 */


const { ccclass, property } = cc._decorator;

export enum Type_node_texiao {
    button_daxiao,   // 按钮 大小 呼吸
    button_wait_2_show,  // 按钮延迟2秒显示
    win_begin_chandou,  // 弹窗 出现 颤抖
    button_jianduan_chandou,    // 按钮 间断颤抖
    win_begin_bianxing,  // 弹窗 出现 变形
    button_jianduan_bianxing,    // 按钮 间断变形
    zheng_xuanzhuan,  // 正旋转
    fan_xuanzhuan,  // 反旋转
    zuoyou_yaohuang, // 左右摇晃
    win_begin_xiaChu, // 窗口下边出现
    opacity_huxi, // 透明度呼吸
    win_begin_opacity_out, // 透明度 255-0 出场
    win_begin_opacity_in,  // 透明度 0-255 入场    
    win_begin_youChu, // 窗口右边出现
}

@ccclass
export default class node_texiao extends cc.Component {

    @property({ type: cc.Enum(Type_node_texiao) })
    type: Type_node_texiao = Type_node_texiao.button_daxiao;

    @property
    time = -1;

    onEnable() {
        switch (this.type) {
            case Type_node_texiao.button_daxiao:
                this.onEnable_button_daxiao();
                break;
            case Type_node_texiao.button_wait_2_show:
                this.onEnable_button_wait_2_show();
                break;
            case Type_node_texiao.win_begin_chandou:
                this.onEnable_win_begin_chandou();
                break;
            case Type_node_texiao.button_jianduan_chandou:
                this.onEnable_button_jianduan_chandou();
                break;
            case Type_node_texiao.win_begin_bianxing:
                this.onEnable_win_begin_bianxing();
                break;
            case Type_node_texiao.button_jianduan_bianxing:
                this.onEnable_button_bianxing();
                break;
            case Type_node_texiao.zheng_xuanzhuan:
                this.onEnable_zheng_xuanzhuan();
                break;
            case Type_node_texiao.fan_xuanzhuan:
                this.onEnable_fan_xuanzhuan();
                break;
            case Type_node_texiao.zuoyou_yaohuang:
                this.onEnable_zuoyou_yaohuang();
                break;
            case Type_node_texiao.win_begin_xiaChu:
                this.onEnable_win_begin_xiaChu();
                break;
            case Type_node_texiao.opacity_huxi:
                this.onEnable_opacity_huxi();
                break;
            case Type_node_texiao.win_begin_opacity_out:
                this.onEnable_begin_opacity_out();
                break;
            case Type_node_texiao.win_begin_opacity_in:
                this.onEnable_begin_opacity_in();
                break;
            case Type_node_texiao.win_begin_youChu:
                this.onEnable_win_begin_youChu();
                break;
        }
    }

    onDisable() {
        switch (this.type) {
            case Type_node_texiao.button_daxiao:
                this.onDisable_button_daxiao();
                break;
            case Type_node_texiao.button_wait_2_show:
                this.onDisable_button_wait_2_show();
                break;
            case Type_node_texiao.win_begin_chandou:
                this.onDisable_win_begin_chandou();
                break;
            case Type_node_texiao.button_jianduan_chandou:
                this.onDisable_button_jianduan_chandou();
                break;
            case Type_node_texiao.win_begin_bianxing:
                this.onDisable_win_begin_bianxing();
                break;
            case Type_node_texiao.button_jianduan_bianxing:
                this.onDisable_button_bianxing();
                break;
            case Type_node_texiao.zheng_xuanzhuan:
                this.onDisable_zheng_xuanzhuan();
                break;
            case Type_node_texiao.fan_xuanzhuan:
                this.onDisable_fan_xuanzhuan();
                break;
            case Type_node_texiao.zuoyou_yaohuang:
                this.onDisable_zuoyou_yaohuang();
                break;
            case Type_node_texiao.win_begin_xiaChu:
                this.onDisable_win_begin_xiaChu();
                break;
            case Type_node_texiao.opacity_huxi:
                this.onDisable_opacity_huxi();
                break;
            case Type_node_texiao.win_begin_opacity_out:
                this.onDisable_begin_opacity_out();
                break;
            case Type_node_texiao.win_begin_opacity_in:
                this.onDisable_begin_opacity_in();
                break;
            case Type_node_texiao.win_begin_youChu:
                this.onDisable_win_begin_youChu();
                break;
        }
    }

    /********************************************
     *          按钮  大小 呼吸
     ********************************************/
    act_button_daxiao = null;
    onEnable_button_daxiao() {
        this.act_button_daxiao = cc.sequence(
            cc.scaleTo(0.7, 1.2),
            cc.scaleTo(0.7, 1)
        ).repeatForever();
        this.node.runAction(this.act_button_daxiao);
    }
    onDisable_button_daxiao() {
        this.node.stopAction(this.act_button_daxiao);
    }

    /*********************************************
     *          按钮 延迟 time 秒 显示
     *********************************************/
    act_button_wait_2_show = null;
    onEnable_button_wait_2_show() {
        this.node.opacity = 0;
        let t = 1.5;
        if (this.time >= 0) {
            t = this.time;
        }
        this.act_button_wait_2_show = cc.sequence(
            cc.delayTime(t),
            cc.fadeIn(0.5),
        );
        this.node.runAction(this.act_button_wait_2_show);
    }

    onDisable_button_wait_2_show() {
        this.node.stopAction(this.act_button_wait_2_show);
    }

    /********************************************
     *          弹窗 出现 颤抖
     ********************************************/
    act_win_begin_chandou = null;
    onEnable_win_begin_chandou() {
        let time = 0.2;
        if (this.time > 0) {
            time = this.time;
        }
        this.node.scale = 0.1;
        this.act_win_begin_chandou = cc.sequence(
            cc.scaleTo(0.5 * time, 1),
            cc.scaleTo(0.3 * time, 0.9),
            cc.scaleTo(0.1 * time, 1),
            cc.scaleTo(0.05 * time, 0.95),
            cc.scaleTo(0.05 * time, 1),
        );
        this.node.runAction(this.act_win_begin_chandou);
    }

    onDisable_win_begin_chandou() {
        this.node.stopAction(this.act_win_begin_chandou);
    }

    /****************************************************
     *          按钮 间断性 颤抖
     ***************************************************/
    act_button_jianduan_chandou = null;
    begin_jiaodu = 0
    onEnable_button_jianduan_chandou() {
        this.begin_jiaodu = this.node.angle;
        let t = 3;
        if (this.time >= 0) {
            t = this.time;
        }
        this.act_button_jianduan_chandou = cc.sequence(
            cc.delayTime(t),
            cc.rotateBy(0.1, 20), //20
            cc.rotateBy(0.1, -40), //-20
            cc.rotateBy(0.1, 35), // 15
            cc.rotateBy(0.1, -25), // -10
            cc.rotateBy(0.1, 15), // 5
            cc.rotateBy(0.1, -5), // 0
        ).repeatForever();
        this.node.runAction(this.act_button_jianduan_chandou);
    }
    onDisable_button_jianduan_chandou() {
        this.node.stopAction(this.act_button_jianduan_chandou);
        this.node.angle = this.begin_jiaodu;
    }

    /*******************************************************
     *           窗口 出现时 变形
     *******************************************************/
    act_win_begin_bianxing = null;
    onEnable_win_begin_bianxing() {
        this.act_win_begin_bianxing = cc.sequence(
            cc.scaleTo(0.1, 1.2),
            cc.scaleTo(0.1, 1.3, 1),
            cc.scaleTo(0.1, 0.9),
            cc.scaleTo(0.1, 1),
        );
        this.node.runAction(this.act_win_begin_bianxing);
    }

    onDisable_win_begin_bianxing() {
        this.node.stopAction(this.act_win_begin_bianxing);
    }

    /******************************************************
     *             按钮 随机 变形
     ******************************************************/
    act_button_bianxing = null;
    onEnable_button_bianxing() {
        let t = 3;
        if (this.time >= 0) {
            t = this.time;
        } else {
            t = 2 + Math.random() * 10 / 5;
        }

        this.act_button_bianxing = cc.sequence(
            cc.delayTime(t),
            cc.scaleTo(0.1, 1.2),
            cc.scaleTo(0.1, 1.3, 1),
            cc.scaleTo(0.1, 0.9),
            cc.scaleTo(0.1, 1)
        ).repeatForever();

        this.node.runAction(this.act_button_bianxing);
    }
    onDisable_button_bianxing() {
        this.node.stopAction(this.act_button_bianxing);
    }

    /*****************************************************
     *             正 旋转  (顺时针)
     *****************************************************/
    act_zheng_xuanzhuan = null;
    onEnable_zheng_xuanzhuan() {
        let t = 1;
        if (this.time >= 0) {
            t = this.time;
        }
        this.act_zheng_xuanzhuan = cc.rotateBy(t, 360).repeatForever();
        this.node.runAction(this.act_zheng_xuanzhuan);
    }
    onDisable_zheng_xuanzhuan() {
        this.node.stopAction(this.act_zheng_xuanzhuan);
    }

    /*****************************************************
     *             反 旋转  (逆时针)
     *****************************************************/
    act_fan_xuanzhuan = null;
    onEnable_fan_xuanzhuan() {
        let t = 1;
        if (this.time >= 0) {
            t = this.time;
        }
        this.act_fan_xuanzhuan = cc.rotateBy(t, -360).repeatForever();
        this.node.runAction(this.act_fan_xuanzhuan);
    }
    onDisable_fan_xuanzhuan() {
        this.node.stopAction(this.act_fan_xuanzhuan);
    }

    /*************************************************
     *            左右摇晃
     *************************************************/
    act_zuoyou_yaohuang = null;
    onEnable_zuoyou_yaohuang() {
        let w = this.node.width;
        let act_1 = cc.moveTo(0.3, -w / 5, 0);
        let act_2 = cc.rotateTo(0.3, -20);

        let act_3 = cc.moveTo(0.3, w / 5, 0)
        let act_4 = cc.rotateTo(0.3, 20);

        this.act_zuoyou_yaohuang = cc.sequence(cc.spawn(act_1, act_2), cc.spawn(act_3, act_4)).repeatForever();
        this.node.runAction(this.act_zuoyou_yaohuang);
    }
    onDisable_zuoyou_yaohuang() {
        this.node.stopAction(this.act_zuoyou_yaohuang);
    }

    /************************************************
     *              窗口 下边 弹出
     ************************************************/
    act_win_begin_xiaChu = null;
    onEnable_win_begin_xiaChu() {
        if (this.time < 0) {
            this.time = 0.1;
        }

        let widget_top = null;
        let widget_bottom = null;
        let widget_mode = null;
        let widget: cc.Widget = this.node.getComponent(cc.Widget);
        if (widget) {
            widget_mode = widget.alignMode;
            widget.alignMode = cc.Widget.AlignMode.ALWAYS;

            if (widget.isAlignTop) {
                widget_top = widget.top;
                widget.isAlignTop = false;
            }
            if (widget.isAlignBottom) {
                widget_bottom = widget.bottom;
                widget.bottom -= this.node.height;
            }

            cc.tween(widget).
                to(this.time, { bottom: widget_bottom })
                .call(() => {
                    if (widget_top != null) {
                        widget.isAlignTop = true;
                    }
                    widget.alignMode = widget_mode;
                })
                .start();
        } else {
            let h = this.node.height;
            this.node.y -= h;
            this.act_win_begin_xiaChu = cc.moveBy(this.time, 0, h);
            this.node.runAction(this.act_win_begin_xiaChu);
        }
    }
    onDisable_win_begin_xiaChu() {
        this.node.stopAction(this.act_win_begin_xiaChu);
    }

    /**************************************************
     *              透明度 呼吸 特效
     **************************************************/
    act_opacity_huxi = null;
    onEnable_opacity_huxi() {
        let time = 1.4;
        if (this.time != -1) {
            time = this.time;
        }
        this.act_opacity_huxi = cc.sequence(
            cc.fadeTo(time / 2, 0),
            cc.fadeTo(time / 2, 255),
        ).repeatForever();
        this.node.runAction(this.act_opacity_huxi);
    }
    onDisable_opacity_huxi() {
        this.node.stopAction(this.act_opacity_huxi);
    }

    /**********************************************
     *          窗口出现 黑屏 融入
     **********************************************/
    onEnable_begin_opacity_out() {
        let t = this.time;
        if (t <= 0) {
            t = 1;
        }
        this.node.opacity = 255;
        cc.tween(this.node)
            .to(t, { opacity: 0 })
            .start();
    }

    onDisable_begin_opacity_out() {

    }

    /**********************************************
     *          窗口出现 
     **********************************************/
    onEnable_begin_opacity_in() {
        let t = this.time;
        if (t <= 0) {
            t = 1;
        }
        this.node.opacity = 0;
        cc.tween(this.node)
            .to(t, { opacity: 255 })
            .start();
    }

    onDisable_begin_opacity_in() {

    }

    /************************************************
     *              窗口 右边 弹出
     ************************************************/
    act_win_begin_youChu = null;
    onEnable_win_begin_youChu() {
        if (this.time < 0) {
            this.time = 0.1;
        }

        let widget_left = null;
        let widget_right = null;
        let widget_mode = null;
        let widget: cc.Widget = this.node.getComponent(cc.Widget);
        if (widget) {
            widget_mode = widget.alignMode;
            widget.alignMode = cc.Widget.AlignMode.ALWAYS;

            if (widget.isAlignLeft) {
                widget_left = widget.left;
                widget.isAlignLeft = false;
            }
            if (widget.isAlignRight) {
                widget_right = widget.right;
                widget.right -= this.node.width;
            }

            cc.tween(widget).
                to(this.time, { right: widget_right })
                .call(() => {
                    if (widget_left != null) {
                        widget.isAlignLeft = true;
                    }
                    widget.alignMode = widget_mode;
                })
                .start();
        } else {
            let w = this.node.width;
            this.node.x += w;
            this.act_win_begin_youChu = cc.moveBy(this.time, -w, 0);
            this.node.runAction(this.act_win_begin_youChu);
        }
    }
    onDisable_win_begin_youChu() {
        this.node.stopAction(this.act_win_begin_youChu);
    }
}
